package com.sheep.spark.util;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Logger;

import java.io.File;

/**
 * Created by Administrator on 2016/3/25.
 */
public class ConfSupport {
    private static final Logger logger = Logger.getLogger(ConfSupport.class);

    private static String sparkName = null;
    //登陆LINUX用户
    private static String loginUser = null;
    //HADOOP地址
    private static String hadoopLocalDir = null;
    //HADOOP地址
    private static String hadoopAddress = null;
    //SPARK地址
    private static String sparkAddress = null;
    //当前JAR包地址
    private static String[] sparkJarAddress = null;

    //ADSB目录中间字符串
    private static String ADSB_MID = null;
    //ADSB目录尾部字符串
    private static String ADSB_END = null;

    //初始化WORDCOUNT.JOB的HDFS文件地址
    private static String initHDFSFile = null;

    //STREAM定时
    private static int streamInterval = -1;
    private static String streamNetworkIP = null;
    private static int streamNetworkPort = -1;

    private static CompositeConfiguration config = new CompositeConfiguration();

    static {
        try {
            String path = "conf/application.properties";
            File f = new File(path);
            if (f.exists()) {
                config.addConfiguration(new PropertiesConfiguration(path));
            }
        } catch (Exception e) {
            logger.error("配置解析失败" + e.getMessage());
        }
    }

    public static CompositeConfiguration getConfig() {
        return config;
    }

    /**
     * 是否需要加载本地HADOOP环境
     */
    public static boolean needLoadLocalHadoop() {
        return ConfSupport.getConfig().getBoolean("hadoop.local", false);
    }

    public static String getSparkName() {
        if (sparkName == null) {
            sparkName = getConfig().getString("spark.name", "SparkName");
        }
        return sparkName;
    }
    /**
     * 获得HADOOP地址
     */
    public static String getHadoopAddress() {
        if (hadoopAddress == null) {
            hadoopAddress = getConfig().getString("hadoop.address", "hdfs://localhost:9000");
        }
        return hadoopAddress;
    }

    /**
     * 获得登陆LINUX用户
     */
    public static String getLoginUser() {
        if (loginUser == null) {
            loginUser = getConfig().getString("login.user", "root");
        }
        return loginUser;
    }

    /**
     * 获得HADOOP本地安装目录
     */
    public static String getLocalHadoopDir() {
        if (hadoopLocalDir == null) {
            hadoopLocalDir = getConfig().getString("hadoop.local.dir", "D:\\hadoop-2.6.4");
        }
        return hadoopLocalDir;
    }

    /**
     * 获得SPARK地址
     */
    public static String getSparkAddress() {
        if (sparkAddress == null) {
            sparkAddress = getConfig().getString("spark.address", "spark://localhost:7077");
        }
        return sparkAddress;
    }

    /**
     * 获得当前JAR包地址
     */
    public static String[] getSparkJarAddress() {
        if (sparkJarAddress == null) {
            sparkJarAddress = getConfig().getString("spark.jar.address",
                    "").split(";");
        }
        return sparkJarAddress;
    }

    //获得ADSB目录中间字符串
    public static String getADSB_MID() {
        if (ADSB_MID == null) {
            ADSB_MID = getConfig().getString("ADSB_MID", "ADSB");
        }
        return ADSB_MID;
    }

    //获得ADSB目录尾部字符串
    public static String getADSB_END() {
        if (ADSB_END == null) {
            ADSB_END = getConfig().getString("ADSB_END", "-m-00000");
        }
        return ADSB_END;
    }

    public static String getInitHDFSFile() {
        if (initHDFSFile == null) {
            initHDFSFile = getConfig().getString("init.hdfs.file", "hdfs://localhost:9000/init/test.txt");
        }
        return initHDFSFile;
    }

    public static int getStreamInterval() {
        if (streamInterval <= 0) {
            streamInterval = getConfig().getInt("stream.interval", 10);
        }
        return streamInterval;
    }

    public static String getStreamNetworkIP() {
        if (streamNetworkIP == null) {
            streamNetworkIP = getConfig().getString("stream.network.ip", "127.0.0.1");
        }
        return streamNetworkIP;
    }

    public static int getStreamNetworkPort() {
        if (streamNetworkPort <= 0) {
            streamNetworkPort = getConfig().getInt("stream.network.port", 9000);
        }
        return streamNetworkPort;
    }
    //获得ADSB文件的HDFS地址
    public static String getADSBFileAddress(String year, String month, String date, String an, String fi) {
        return ConfSupport.getHadoopAddress() + "/" + getADSB_MID() + "/" + year + "/" + month + "/" + date + "/" + an + "_" + fi + getADSB_END();
    }

}
